import math

class Solution(object):
    def isPowerOfThree(self, n):
        """
        :type n: int
        :rtype: bool
        """
        if n <= 0:
            return False

        l = math.log10(n) / math.log10(3)

        return (l - int(l)) == 0

s = Solution()
print(s.isPowerOfThree(27))
print(s.isPowerOfThree(6))
print(s.isPowerOfThree(4))
print(s.isPowerOfThree(9))
print(s.isPowerOfThree(243))